package net.wicp.tams.common.redis.cachecloud;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.http.HttpClient;
import net.wicp.tams.common.http.HttpResult;
import net.wicp.tams.common.redis.RedisAssit;
import net.wicp.tams.common.redis.pool.AbsPool;
import net.wicp.tams.common.redis.pool.JedisPoolTams;
import net.wicp.tams.common.redis.pool.JedisSentinelPoolTams;
import redis.clients.jedis.Jedis;

@Slf4j
public abstract class CacheCloudAssit {
	public static AbsPool standalone(long appId, GenericObjectPoolConfig<Jedis> poolConfig) {
		while (true) {
			String url = String.format(
					Conf.get("common.redis.cachecloud.domain_url") + Conf.get("common.redis.cachecloud.redis_standalone_suffix") + Conf.get("common.redis.cachecloud.client_version"), appId);
			HttpResult result = HttpClient.doGet(url);
			if (result == null || StringUtil.isNull(result.getBodyStr())) {
				log.warn("this http return error,url:[{}]", url);
				continue;
			}
			JSONObject parseObject = JSONObject.parseObject(result.getBodyStr());
			String instance = parseObject.getString("standalone");
			String[] instanceArr = instance.split(":");
			if (instanceArr.length != 2) {
				log.warn("instance info is invalid, instance: {}, appId: {}, continue...", instance, appId);
				continue;
			}
			JedisPoolTams standalone = RedisAssit.standalone(instanceArr[0], Integer.parseInt(instanceArr[1]),
					poolConfig);
			return standalone;
		}
	}

	public static AbsPool standalone(long appId) {
		return standalone(appId, null);
	}

	public static AbsPool sentinel(long appId, GenericObjectPoolConfig<Jedis> poolConfig) {
		while (true) {
			/**
			 * http请求返回的结果是空的；
			 */
			String url = String.format(
					Conf.get("common.redis.cachecloud.domain_url") + Conf.get("common.redis.cachecloud.redis_sentinel_suffix") + Conf.get("common.redis.cachecloud.client_version"), appId);
			HttpResult result = HttpClient.doGet(url);
			if (result == null || StringUtil.isNull(result.getBodyStr())) {
				log.warn("this http return error,url:[{}]", url);
				continue;
			}
			JSONObject parseObject = JSONObject.parseObject(result.getBodyStr());
			if (parseObject == null || parseObject.size() == 0) {
				log.error("get sentinel info for appId: {} error. continue...", appId);
				continue;
			}

			/** 检查客户端版本 **/
			int status = parseObject.getIntValue("status");
			if (status == ClientStatusEnum.ERROR.getStatus()) {
				throw new IllegalStateException(parseObject.getString("message"));
			} else if (status == ClientStatusEnum.WARN.getStatus()) {
				log.warn(parseObject.getString("message"));
			} else {
				log.info(parseObject.getString("message"));
			}

			/**
			 * 有效的请求：取出masterName和sentinels，并创建JedisSentinelPool的实例；
			 */
			String masterName = parseObject.getString("masterName");
			String sentinels = parseObject.getString("sentinels");

			JedisSentinelPoolTams sentinel = RedisAssit.sentinel(masterName, sentinels.split(" "), poolConfig);
			return sentinel;
		}
	}

	public static AbsPool sentinel(long appId) {
		return sentinel(appId, null);
	}
}
